/**    
 * 文件名：ContentFinder.java    
 *    
 * 版本信息：    
 * 日期：2014年3月10日    
 * xyj 足下 xyj 2014     
 * 版权所有    
 *    
 */
package opencvtest.chapter04;

import static com.googlecode.javacv.cpp.opencv_core.IPL_DEPTH_32F;
import static com.googlecode.javacv.cpp.opencv_core.cvGetSize;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_THRESH_BINARY;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvCalcBackProject;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvThreshold;
import static opencvtest.OpenCVUtils.toIplImage32F;
import static opencvtest.OpenCVUtils.toIplImage8U;

import com.googlecode.javacv.cpp.opencv_core.IplImage;
import com.googlecode.javacv.cpp.opencv_imgproc.CvHistogram;

/**
 * @项目名称：opencv-test
 * @类名称：ContentFinder
 * @类描述：
 * @创建人：zhuyi
 * @创建时间：2014年3月10日 下午3:13:09
 * @修改人：zhuyi
 * @修改时间：2014年3月10日 下午3:13:09
 * @修改备注：
 * @version
 * 
 */
public class ContentFinder {

    public float threshold = -1f;

    public CvHistogram histogram;

    public IplImage find(IplImage image) {

        // Split the input image into channels, each image passed to
        // `cvCalcBackProject` must be single channel.
        // Convert each channel image to a to 32 bit floating point image.
        IplImage[] channels = ColorHistogram.splitChannels(image);

        for (int i = 0; i < channels.length; i++) {
            channels[i] = toIplImage32F(channels[i]);
        }

        // Back project
        IplImage dest = IplImage.create(cvGetSize(image), IPL_DEPTH_32F, 1);
        cvCalcBackProject(channels, dest, histogram);

        if (threshold > 0) {
            cvThreshold(dest, dest, threshold, 1, CV_THRESH_BINARY);
        }

        return toIplImage8U(dest, true);
    }
}
